<?php
ini_alter("max_execution_time","3600");
ini_alter("default_socket_timeout","3600"); 

include_once(APPLICATION_ROOT."/web_config.php");
include_once(APPLICATION_ROOT."/config.php");
require_once(APPLICATION_ROOT."/oracleaccess/classes/DbConnect.php" );
require_once(APPLICATION_ROOT."/logs/LogApp.class" );

$connessione = new DbConnect();//connessione viste locale

$data_abbinamento=date('Ymd');

$select_recs="SELECT CIG,PROG_CUI,ID_SIMOG,SCHEDA,DATA_ID,DENOM_STAZIONE_APPALTANTE,CF_RUP,ID_SCHEDA_LOCALE,CF_UTENTE,ID FROM AVCP WHERE SCHEDA='COLLAUDO' and id_scheda_locale is null order by cig,prog_cui,ID_SIMOG asc";
$query_recs = $connessione -> doquery_oracle($select_recs, 'select');

for($r=1; $r<=(count($query_recs)); $r++){

 $cig_xls=$prog_cui_xls=$id_simog_xls=$scheda_db=$data_id_xls=$resultUDC = "";
 $cig_xls=     $query_recs[$r][1];
 $prog_cui_xls=$query_recs[$r][2];
 $id_simog_xls=$query_recs[$r][3];
 $scheda_db=   $query_recs[$r][4];
 $data_id_xls= $query_recs[$r][5];
 $id_tabella_avcp= $query_recs[$r][10];  

  //SELECT ID SCHEDA LOCALE
  $sel="SELECT ID FROM  COLLAUDO WHERE cig='".$cig_xls."' AND NUM_PROGR='".$prog_cui_xls."'";
  $rs = $connessione->doquery_oracle($sel, 'select');
  
  if($rs[1][1]!=""){ 
   $id_scheda_locale=$rs[1][1];
   
   $updateAGG="update COLLAUDO set ID_SIMOG='".$id_simog_xls."', SCHEDA_CHIUSA='I', ARCHIVIATA='S' WHERE id='".$id_scheda_locale."' ";
   $connessione->doquery_oracle($updateAGG, 'update');  

   $update_AVCP="update AVCP set ID_SCHEDA_LOCALE='".$id_scheda_locale."',DATA_ABBINAMENTO='".$data_abbinamento."' WHERE ID='".$id_tabella_avcp."'";
   $connessione->doquery_oracle($update_AVCP, 'update');    
  }else{
   $message["SCHEDA"]="COLLAUDO";
   $message["CIG"]=$cig_xls;
   $message["NUM_PROGR"]="";
   $message["NUM_PROGR_SCHEDA"]="";
   $message["OPERAZIONE"]=$sel; 
   $message["MSG"]="Update fallita: non esiste il CIG.";
   LogApp::getInstance()->LogWrite($message);   
  }  
 
}

echo("<br>END COLLAUDO");

?>